//Grep.java
package decorator.halfClear.grep;

import java.io.*;

/**
 *@author:youtubezou
 *@version:2012-7-18
 */
public class Grep {
	private static GrepView view = new GrepView();
	private static GrepReader in;

	public static void main(String[] args) throws IOException {
		String line;
		String substr;
		String fileName;
		BufferedReader stdin = new BufferedReader(new InputStreamReader((System.in)));
		boolean isDone = false;
		
		while(!isDone){
			do{
				System.out.print("Please input filename(path divid by /):");
			}while((fileName = stdin.readLine()) == null);
			
			do{
				System.out.print("Please input search string:");
			}while((substr = stdin.readLine()) == null);
			
			in = new GrepReader(new FileReader(fileName), substr);
			
			while(true){
				line = in.readLine();
				if(line == null) {
					break;
				}
				view.println(fileName + ":" + in.getLineNo() + "\t" + line);
			}
						
			System.out.print("Do you want to try again[y/n]:");
			line = stdin.readLine();
			if(line.equalsIgnoreCase("y")){
				isDone = false;
			} else {
				isDone = true;
			}
			
		}
	}

}
